package com.cts.learninglibrary.config;

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.sql.Timestamp;
import java.util.Date;

/**
 * @author lpf
 */
@Configuration
@EnableSwagger2
@ConditionalOnProperty(name = "swagger.enable", havingValue = "true")
public class SwaggerConfig {
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2).apiInfo(arctiqueApiInfo())
                .groupName("arctique")
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.cts.learninglibrary.controller"))
                .paths(PathSelectors.any())
                         //避免Swagger生成无用的Timestamp字段实体对象
                .build().directModelSubstitute(Timestamp.class, Date.class);
    }


    private ApiInfo arctiqueApiInfo() {
        return new ApiInfoBuilder()
                .title("接口文档")
                .description("学习库系统项目后端接口介绍")
                .contact(new Contact("arctique", "http://arctique.cn", "626165573@qq.com"))
                .termsOfServiceUrl(null)
                .version("1.0").build();
    }
}